package cn.com.bluemoon.daps.system.controller.api;

import cn.com.bluemoon.daps.api.sys.RemoteSystemBizLineService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiStandard;
import cn.com.bluemoon.daps.common.datascope.IgnorePermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.system.entity.DapSystemBizLine;
import cn.com.bluemoon.daps.system.entity.DapSystemBizLineDetail;
import cn.com.bluemoon.daps.system.service.DapSystemBizLineDetailService;
import cn.com.bluemoon.daps.system.service.DapSystemBizLineService;
import cn.com.bluemoon.daps.system.vo.BizLineDetailResponseVo;
import cn.com.bluemoon.daps.system.vo.BizLineResponseVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务接口生产类-内部调用
 *
 * @author Jarod.Kong
 */
@Api(tags = {"FeignClient服务-业务线Api"})
@RestController
@RequestMapping(DapsApiStandard.API_SYS_BIZLINE)
@IgnorePermission(name = "服务注册-业务线")
public class DapsSysBizLineApi implements RemoteSystemBizLineService {
    @Resource
    private DapSystemBizLineService bizLineService;

    @Resource
    private DapSystemBizLineDetailService bizLineDetailService;

    @Override
    public ResultBean<List<BizLineResponseVo>> bizLines() {
        return ResultBean.ok(bizLineService.list(bizLineService.bmLambdaQuery()))
                .convert(bizLines -> bizLines.stream()
                        .map(BizLineResponseVo::new).map(bizLineService::appendUserInfoExtra)
                        .collect(Collectors.toList()));
    }

    @Override
    public ResultBean<BizLineResponseVo> getById(String bizLineId) {
        return ResultBean.ok(bizLineService.getById(bizLineId))
                .convert(BizLineResponseVo::new).convert(bizLineService::appendUserInfoExtra);
    }

    @Override
    public ResultBean<List<BizLineResponseVo>> bizLinesByDataManagerUserId(String userId) {
        LambdaQueryWrapper<DapSystemBizLine> eq = bizLineService.bmLambdaQuery().eq(DapSystemBizLine::getDataManager, userId);
        List<BizLineResponseVo> res = bizLineService.list(eq).stream().map(BizLineResponseVo::new)
                .map(bizLineService::appendUserInfoExtra).collect(Collectors.toList());
        return ResultBean.ok(res);
    }

    @Override
    public ResultBean<List<BizLineResponseVo>> bizLinesByDataManagerOrAdminUserId(String userId) {
        List<DapSystemBizLineDetail> details = bizLineDetailService.findByAdminUserId(userId);
        Set<Integer> bizLineIds = details.stream().map(DapSystemBizLineDetail::getBizLineId).collect(Collectors.toSet());
        LambdaQueryWrapper<DapSystemBizLine> bizLineWrapper = bizLineService.bmLambdaQuery().eq(DapSystemBizLine::getDataManager, userId);
        bizLineIds.addAll(bizLineService.list(bizLineWrapper).stream().map(DapSystemBizLine::getBizLineId).collect(Collectors.toList()));
        List<BizLineResponseVo> res = bizLineResponseVosByBizLineIds(bizLineIds);
        return ResultBean.ok(res);
    }

    @Override
    public ResultBean<List<BizLineResponseVo>> bizLinesByAdminUserId(String userId) {
        Set<Integer> bizLineIds = bizLineDetailService.findByAdminUserId(userId).stream().map(DapSystemBizLineDetail::getBizLineId).collect(Collectors.toSet());
        return ResultBean.ok(bizLineResponseVosByBizLineIds(bizLineIds));
    }

    @Override
    public ResultBean<List<BizLineResponseVo>> bizLinesByAdminOrDevelopUserId(String userId) {
        Set<Integer> bizLineIds = bizLineDetailService.findByAdminOrDevelopUserId(userId).stream().map(DapSystemBizLineDetail::getBizLineId).collect(Collectors.toSet());
        return ResultBean.ok(bizLineResponseVosByBizLineIds(bizLineIds));
    }

    @Override
    public ResultBean<List<BizLineDetailResponseVo>> bizLineDetailsByBizLineIdAndSystemId(@Nullable Integer bizLineId, String systemId) {
        LambdaQueryWrapper<DapSystemBizLineDetail> eq = bizLineDetailService.bmLambdaQuery().eq(bizLineId != null, DapSystemBizLineDetail::getBizLineId, bizLineId)
                .eq(DapSystemBizLineDetail::getSystemId, systemId);
        List<DapSystemBizLineDetail> details = bizLineDetailService.list(eq);
        List<BizLineDetailResponseVo> res = details.stream().map(d ->
                bizLineDetailService.toBizLineDetailResponseVo(d)).collect(Collectors.toList());
        return ResultBean.ok(res);
    }

    private List<BizLineResponseVo> bizLineResponseVosByBizLineIds(Set<Integer> bizLineIds) {
        if (bizLineIds.isEmpty()) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<DapSystemBizLine> eq = bizLineService.bmLambdaQuery()
                .in(DapSystemBizLine::getBizLineId, bizLineIds);
        List<BizLineResponseVo> res = bizLineService.list(eq)
                .stream().map(BizLineResponseVo::new).map(bizLineService::appendUserInfoExtra).collect(Collectors.toList());
        return res;
    }
}
